import { ipcMain, type BrowserWindow } from 'electron';
// import { PosPrinter } from 'electron-pos-printer';
import electronPosPrinter from 'electron-pos-printer';
// import HID from 'node-hid';

const { PosPrinter } = electronPosPrinter;

console.log('打印机模块');

interface PrintOptions {
  mainWindow: BrowserWindow;
}

export function usePrintHooks(props: PrintOptions) {
  const { mainWindow: win } = props;
  // 获取所有打印机列表
  const handleGetDeviceList = async (_event: any, options: any) => {
    // 获取所有连接的HID设备
    // const devices = HID.devices();
    // return devices;

    return win.webContents.getPrintersAsync();
  };

  // 发送参数到打印机
  const onSendUsbPrinter = async (event: any, info: any) => {
    return PosPrinter.print(info.data, info.options);
  };

  ipcMain.on('onSendUsbPrinter', onSendUsbPrinter);
  ipcMain.handle('getPrinters', handleGetDeviceList);
  return {};
}
